package own.stu.jobgib.playown.netty.im.client.v2;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import own.stu.jobgib.playown.netty.im.client.common.ConsoleHandler;

import java.util.Scanner;

public class ClientHandlersInitializer extends ChannelInitializer<SocketChannel> {

    private ConsoleHandler consoleHandler;
    private ReconnectHandler reconnectHandler;

    public ClientHandlersInitializer(Client_v2 client_v2) {
        this.consoleHandler = new ConsoleHandler(client_v2.getExecutor());
        this.reconnectHandler = new ReconnectHandler(client_v2);
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(reconnectHandler);
        pipeline.addLast(new StringDecoder());
        pipeline.addLast(new StringEncoder());
        pipeline.addLast(consoleHandler);
        pipeline.addLast(new Pinger());
    }
}